package com.aia.ann.web.xml;

import java.io.File;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.poi.util.IOUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.aia.ann.domain.Agent;
import com.aia.ann.service.ChiefService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class AnnUploadXml extends ActionSupport implements SessionAware {
	private static final Logger logger = Logger.getLogger(AnnUploadXml.class);
	private static final long serialVersionUID = 1L;
	private Map session;
//	private File imgcode;
//	private String imgcodeFileName;
	private ChiefService chiefService;
	private String xml;

	private byte[] imgdata;
	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;
	}

	public void setChiefService(ChiefService chiefService) {
		this.chiefService = chiefService;
	}

	public ChiefService getChiefService() {
		return chiefService;
	}

//	public void validate() {
//		logger.debug("imageFileName:" + imgcodeFileName);
//		logger.debug("imgcode is null?:" + ((imgcode != null) ? "not null" : "null"));
//		if (imgcode == null) {
//			this.addActionError("imgcode is null");
//			return;
//		}
//
//		if (imgcode.length() > 1024 * 1024) {
//			this.addActionError("您选择的文件大小超过了1024KB");
//			return;
//		}
//	}

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("AnnUploadXml - start"); //$NON-NLS-1$
		}
		
		
		
		byte[] data = null;
		try {
			HttpServletRequest req = ServletActionContext.getRequest();
			ServletInputStream input = req.getInputStream();
			data = IOUtils.toByteArray(input);
			imgdata = data;
		} catch (Exception e) {
			logger.error("Cannot read InputStream of request", e);
		}
		try {
			FileUtils.writeByteArrayToFile(new File("D:/annv1.png"), data);
		} catch (Exception e) {
		 logger.warn("Cannot save img to D:/annv1.png", e);
		}
		

		Agent cust = null;
		try {
			cust = (Agent) session.get("customer");
			if (cust == null) {
				addActionError("没有登录或会话超时");
				return LOGIN;
			}
		} catch (Exception e) {
			logger.error("Unknown Error:" + e, e);
			throw new RuntimeException(e);
		}

		try {
			if (logger.isDebugEnabled()) {
				logger.debug("FileUtils.copyFile(imgcode, new File(D:/a.jpg));"); //$NON-NLS-1$
			}
//			try {
//				FileUtils.copyFile(imgcode, new File("D:/ann.jpg"));
//			} catch (Exception e) {
//				logger.warn("Unknown Error:" + e, e);
//			}

			chiefService.annUploadXml(this);

			// xml="<?xml version=\"1.0\" encoding=\"utf-8\"?><data>"+cust.getAG_USERID()+"</data>";

			if (logger.isDebugEnabled()) {
				logger.debug("AnnUploadXml - end"); //$NON-NLS-1$
			}

			return Tools.smartReturn((ActionSupport) this);
		} catch (Exception e) {
			logger.error("Unknown Error:" + e, e);
			throw new RuntimeException(e);
		}
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public String getXml() {
		return xml;
	}

//	public void setImgcodeFileName(String imgcodeFileName) {
//		this.imgcodeFileName = imgcodeFileName;
//	}
//
//	public String getImgcodeFileName() {
//		return imgcodeFileName;
//	}
//
//	public void setImgcode(File imgcode) {
//		this.imgcode = imgcode;
//	}
//
//	public File getImgcode() {
//		return imgcode;
//	}

	public void setImgdata(byte[] imgdata) {
		this.imgdata = imgdata;
	}

	public byte[] getImgdata() {
		return imgdata;
	}

}
